import os
import random
import re
import time
import urllib.request
from urllib.request import urlopen
import threading
from bs4 import BeautifulSoup

file_path = 'H:\\audio'
errorTime = []
hds = [{'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}, \
       {
           'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'}, \
       {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'}]
pattern = re.compile(r'.*a={m4a:"(http.*?m4a)"', re.MULTILINE | re.DOTALL)
mp3_pattern = re.compile(r'.*a={mp3:"(https.*?mp3\\?)"', re.MULTILINE | re.DOTALL)


def downloadFile(url, t, postfix):
    print(url)
    try:
        if not os.path.exists(file_path):
            os.makedirs(file_path)
        filename = '{}\\鬼谷子的局_{}回.{}'.format(file_path, t, postfix)
        # req = urllib.request.Request(url, headers=hds[random.randint(0, 2)])
        # resp = urlopen(req).read()
        urllib.request.urlretrieve(url, filename);
        # binfile = open(filename, 'rb+')
        # binfile.write(resp)
        # binfile.close()
        # urllib.request.urlretrieve(url, filename,Schedule)
    except IOError as e:
        print("IOError")
        errorTime.append(t)
    except Exception as e:
        print("Exception ", e)
        errorTime.append(t)


def Schedule(a, b, c):
    per = 100.0 * a * b / c
    if per > 100:
        per = 100
    print('%.2f%%' % per)


def isContainsTheFile(index):
    for filename in os.listdir(file_path):
        if filename == "鬼谷子的局_{}回.m4a".format(index) or filename == "鬼谷子的局_{}回.mp3".format(index):
            return True
    return False


def fetchtargets(t):
    for i in t:
        print('Current is ', i)
        if (isContainsTheFile(i) == False):
            time.sleep(random.randint(1, 10))
            url = 'https://ting55.com/book/211-' + str(i)
            req = urllib.request.Request(url, headers=hds[random.randint(0, 2)])
            source_code = urlopen(req, timeout=20).read()
            plain_text = str(source_code)
            soup = BeautifulSoup(plain_text, "html.parser")
            script = soup.find('script', text=pattern)
            if script:
                match = pattern.search(script.text)
                if match:
                    downloadFile(match.group(1), str(i), 'm4a')
                else:
                    errorTime.append(str(i))
            else:
                mp3script = soup.find('script', text=mp3_pattern)
                if mp3script:
                    mp3match = mp3_pattern.search(mp3script.text)
                    if mp3match:
                        downloadFile(mp3match.group(1), str(i), 'mp3')
                    else:
                        errorTime.append(str(i))


#fetchtargets(range(1, 492))
#print('Done')
#print(errorTime)
#fetchtargets(range[errorTime])


class jobThread(threading.Thread):
    def __init__(self, threadID, name, tupl):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.tupl = tupl

    def run(self):
        print("开始线程：" + self.name)
        fetchtargets( self.tupl)
        print("退出线程：" + self.name)
		
for x in range(164):
    tup1 = (3*x+1,3*x+2,3*x+3)
    jobThread(x+1,'fetchJob {}'.format(x+1),tup1).start()
